package com.example.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class ShiChuangActivity extends Activity implements View.OnClickListener {

    private TextView fanh;
    private ListView listView;
    private TextView zw;
    private TextView xs;
    private List<MenUsers> list;
    private int i=0;
    private int j=0;
    private myListView myListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shi_chuang);
        initView();
        ititData();
        fanh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShiChuangActivity.this, MainActivity.class));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.xs:
                zw.setText("职位");
                if(i==1){
                    xs.setText("薪水⬇");
                    Collections.reverse(list.get(0).getData());
                    i=0;
                }else{
                    xs.setText("薪水⬆");
                Collections.sort(list.get(0).getData(), new Comparator<MenUsers.DataBean>() {
                            @Override
                            public int compare(MenUsers.DataBean dataBean, MenUsers.DataBean t1) {
                             return dataBean.getSalary()-t1.getSalary();
                            }
                        });
                        i=1;
                }
                break;
            case R.id.zw:
                xs.setText("薪水");
                if(j==1){
                    zw.setText("职位⬇");
                    Collections.reverse(list.get(0).getData());
                    j=0;
                }else{
                    zw.setText("职位⬆");
                Collections.sort(list.get(0).getData(), new Comparator<MenUsers.DataBean>() {
                    @Override
                    public int compare(MenUsers.DataBean dataBean, MenUsers.DataBean t1) {
                        return dataBean.getCls().compareTo(t1.getCls());
                    }
                });
                    j=1;
                }
                break;
        }
        myListView.notifyDataSetChanged();
    }

    private interface BlogServer {
        @GET("getEmpMarketList")
        Call<ResponseBody> getBlog();
    }

    private void ititData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogServer blogServer = retrofit.create(BlogServer.class);
        Call<ResponseBody> blog = blogServer.getBlog();
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    list = new ArrayList<>();
                    String s = response.body().string();
                    MenUsers users = new Gson().fromJson(s, MenUsers.class);
                    list.add(users);
                    myListView = new myListView(list);
                    listView.setAdapter(myListView);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {
        fanh = (TextView) findViewById(R.id.fanh);
        listView = (ListView) findViewById(R.id.listView);
        zw = (TextView) findViewById(R.id.zw);
        zw.setOnClickListener(this);
        xs = (TextView) findViewById(R.id.xs);
        xs.setOnClickListener(this);
    }

    private class myListView extends BaseAdapter {
        List<MenUsers> list;

        public myListView(List<MenUsers> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view1, ViewGroup viewGroup) {
            View view = View.inflate(ShiChuangActivity.this, R.layout.layout2, null);
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView ganwei = (TextView) view.findViewById(R.id.ganwei);
            TextView qian = (TextView) view.findViewById(R.id.qian);
            name.setText(list.get(0).getData().get(i).getName());
            ganwei.setText(list.get(0).getData().get(i).getCls());
            qian.setText(list.get(0).getData().get(i).getSalary() + "");
            return view;
        }
    }
}
